package com.zoke.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.zoke.state.view.PageStateView;

/**
 * @author 大熊
 *         页面状态的简单示例
 */
public class MainActivity extends AppCompatActivity {

    private PageStateView mPageStateView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPageStateView = findView(R.id.pageState);
        mPageStateView.getView(PageStateView.VIEW_STATE_ERROR).findViewById(R.id.retryBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPageStateView.setViewState(PageStateView.VIEW_STATE_LOADING);
            }
        });

    }

    /**
     * 免强转
     *
     * @param resId
     * @param <T>
     * @return
     */
    private <T extends View> T findView(int resId) {
        return (T) findViewById(resId);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.error:
                mPageStateView.setViewState(PageStateView.VIEW_STATE_ERROR);
                return true;

            case R.id.empty:
                mPageStateView.setViewState(PageStateView.VIEW_STATE_EMPTY);
                return true;

            case R.id.content:
                mPageStateView.setViewState(PageStateView.VIEW_STATE_CONTENT);
                return true;

            case R.id.loading:
                mPageStateView.setViewState(PageStateView.VIEW_STATE_LOADING);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
